package com.prs.crm.action.crm;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.annotation.Clew;
import com.prs.crm.domain.crm.CustomerLevel;
import com.prs.crm.domain.crm.FeedbackType;
import com.prs.crm.service.crm.FeedbackTypeService;
import com.prs.crm.service.sys.LogService;

public class FeedbackTypeAction extends BaseAction{
	private FeedbackType feedbackType;
	private FeedbackTypeService feedbackTypeService;
	private PaginationSupport pagination;
	private Integer[] ids;
	private LogService logService;

	@Action("listFeedbackType")
	public String listFeedbackType() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(FeedbackType.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getFeedbackTypeService().listByPage(criteria, pagination));
		return SUCCESS;
	}
	
	@Action("editFeedbackType")
	public String editFeedbackType() {
		return SUCCESS;
	}
	
	@Clew(results={"success"})
	@Action(value="saveFeedbackType",results={@Result(name="success",type="redirect",location="listFeedbackType"),
			@Result(name="error",type="chain",location="editFeedbackType")})
	public String saveFeedbackType() {
		if(feedbackType.getName()==null||"".equals(feedbackType.getName().trim())){
			this.addActionError("类型名称不能为空");
			return ERROR;
		}if (this.getLogService().getSearchName(FeedbackType.class, feedbackType
				.getName())) {
			this.addActionError("名称已经存在");
			return ERROR;
		}
		this.getFeedbackTypeService().save(feedbackType);
		this.setPrompt("提交类型成功");
		return SUCCESS;
	}
	
	@Action(value="removeFeedbackType",results={@Result(name="success",type="redirect",location="listFeedbackType"),
			@Result(name="error",type="chain",location="editFeedbackType")})
	public String removeFeedbackType() {
		if(feedbackType.getName()==null||"".equals(feedbackType.getName().trim())){
			this.addActionError("类型名称不能为空");
			return ERROR;
		}
		if(ids == null) {
			this.addActionError("请选择要删除的类型");
			return ERROR;
		}
		for(Integer id : ids) {
			FeedbackType type = this.getFeedbackTypeService().get(id);
			if(type.getFeedbacks().size() != 0) {
				this.addActionError("类型已被应用，不能删除");
				return ERROR;
			}
		}
		this.getFeedbackTypeService().batchRemove(ids);
		return SUCCESS;
	}
	
	public FeedbackType getFeedbackType() {
		return feedbackType;
	}
	public void setFeedbackType(FeedbackType feedbackType) {
		this.feedbackType = feedbackType;
	}
	public FeedbackTypeService getFeedbackTypeService() {
		return feedbackTypeService;
	}
	public void setFeedbackTypeService(FeedbackTypeService feedbackTypeService) {
		this.feedbackTypeService = feedbackTypeService;
	}
	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
	public Integer[] getIds() {
		return ids;
	}
	public void setIds(Integer[] ids) {
		this.ids = ids;
	}
	

	public LogService getLogService() {
		return logService;
	}

	public void setLogService(LogService logService) {
		this.logService = logService;
	}
}
